package servlet.admin;

import net.sf.json.JSONObject;
import service.admin.BooksService;
import service.admin.impl.BooksServiceImpl;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.PrintWriter;

/**
 * booksDelServlet
 *
 * @author 谭文斌
 * @since 2023/9/4
 */
@WebServlet("/admin/booksDelServlet")
public class BooksDelServlet extends HttpServlet {
    BooksService service=new BooksServiceImpl();

    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        doPost(req, resp);
    }

    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        resp.setContentType("application/json; charset=utf8");
        String id = req.getParameter("id");
        JSONObject json = new JSONObject();
        int code = 1;
        String msg = "";
        int ids=service.del(Integer.valueOf(id));
        if( ids == 1 ) {
            code = 0;
            msg = "删除成功";
        }else {
            code = 1;
            msg = "删除失败";
        }
        json.put("code", code);
        json.put("msg", msg);
        PrintWriter out = resp.getWriter();
        out.print( json.toString() );
    }
}
